﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Sac.Web;
public partial class Admin_UserManage : System.Web.UI.Page
{
    RolePowerOperate role = new RolePowerOperate();
    UserRoleOperate user = new UserRoleOperate();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dropRoleSelect.DataTextField = "Role_Name";
            dropRoleSelect.DataValueField = "ID_KEY";
            dropRoleSelect.DataSource = role.GetAllRole();
            dropRoleSelect.DataBind();
            gridUserList.DataSource = user.GetUsersByRoleIdKey(dropRoleSelect.SelectedValue);
            gridUserList.DataBind();
        }
    }

    public string ConverToRoleName(string roleId)
    {
        return role.GetRoleNameById(roleId);
    }

    protected void dropRoleSelect_SelectedIndexChanged(object sender, EventArgs e)
    {
        gridUserList.DataSource = user.GetUsersByRoleIdKey(dropRoleSelect.SelectedValue);
        gridUserList.DataBind();
    }

    protected void btnSure_Click(object sender, EventArgs e)
    {
        string message = "";
        UserRole userInfo = new UserRole();
        userInfo.RoleIdKey = dropRoleSelect.SelectedValue;
        userInfo.UserName = textUserName.Text.Trim();
        if (user.InsertUser(userInfo))
        {
            message = "添加用户成功！";
            gridUserList.DataSource = user.GetUsersByRoleIdKey(dropRoleSelect.SelectedValue);
            gridUserList.DataBind();
        }
        else
        {
            message = "添加用户失败，请检查该用户是否已经存在！";
        }
        Response.Write("<script>alert('" + message + "')</script>");
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        textUserName.Text = "";
    }

    protected void gridUserList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string userId = gridUserList.DataKeys[e.RowIndex].Value.ToString();
        string message = "";
        if (user.DeleteUserById(userId))
        {
            gridUserList.DataSource = user.GetUsersByRoleIdKey(dropRoleSelect.SelectedValue);
            gridUserList.DataBind();
            message = "删除用户成功！";
        }
        else
        {
            message = "删除失败，请重新尝试！";
        }
        Response.Write("<script>alert('" + message + "')</script>");
    }

    protected void gridUserList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string userId = gridUserList.DataKeys[e.NewSelectedIndex].Value.ToString();
        string message = "";
        if (user.ResetPassword(userId))
        {
            message = "重置密码成功！";
        }
        else
        {
            message = "重置密码失败，请重新尝试！";
        }
        Response.Write("<script>alert('" + message + "')</script>");
    }
}
